<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Introduction to OpenCV Development with Clojure</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../df/d65/tutorial_table_of_content_introduction.html">Introduction to OpenCV</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Introduction to OpenCV Development with Clojure </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Prev Tutorial:</b> <a class="el" href="../../d1/d0a/tutorial_java_eclipse.html">Using OpenCV Java with Eclipse</a></p>
<p><b>Next Tutorial:</b> <a class="el" href="../../d9/d3f/tutorial_android_dev_intro.html">Introduction into Android Development</a></p>
<table class="doxtable">
<tr>
<th align="right"></th><th align="left"></th></tr>
<tr>
<td align="right">Original author </td><td align="left">Mimmo Cosenza </td></tr>
<tr>
<td align="right">Compatibility </td><td align="left">OpenCV &gt;= 3.0 </td></tr>
</table>
<dl class="section warning"><dt>Warning</dt><dd>This tutorial can contain obsolete information.</dd></dl>
<p>As of OpenCV 2.4.4, OpenCV supports desktop Java development using nearly the same interface as for Android development.</p>
<p><a href="http://clojure.org/">Clojure</a> is a contemporary LISP dialect hosted by the Java Virtual Machine and it offers a complete interoperability with the underlying JVM. This means that we should even be able to use the Clojure REPL (Read Eval Print Loop) as and interactive programmable interface to the underlying OpenCV engine.</p>
<h2>What we'll do in this tutorial </h2>
<p>This tutorial will help you in setting up a basic Clojure environment for interactively learning OpenCV within the fully programmable CLojure REPL.</p>
<h3>Tutorial source code</h3>
<p>You can find a runnable source code of the sample in the <code>samples/java/clojure/simple-sample</code> folder of the OpenCV repository. After having installed OpenCV and Clojure as explained in the tutorial, issue the following command to run the sample from the command line. </p><div class="fragment"><div class="line">cd path/to/samples/java/clojure/simple-sample</div><div class="line">lein run</div></div><!-- fragment --> <h2>Preamble </h2>
<p>For detailed instruction on installing OpenCV with desktop Java support refer to the <a class="el" href="../../d9/d52/tutorial_java_dev_intro.html">corresponding</a>tutorial".</p>
<p>If you are in hurry, here is a minimum quick start guide to install OpenCV on Mac OS X:</p>
<dl class="section note"><dt>Note</dt><dd>I'm assuming you already installed <a href="https://developer.apple.com/xcode/">xcode</a>, <a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">jdk</a> and <a href="http://www.cmake.org/cmake/resources/software.html">Cmake</a>.</dd></dl>
<div class="fragment"><div class="line">cd ~/</div><div class="line">mkdir opt</div><div class="line">git clone https://github.com/opencv/opencv.git</div><div class="line">cd opencv</div><div class="line">git checkout 2.4</div><div class="line">mkdir build</div><div class="line">cd build</div><div class="line">cmake -DBUILD_SHARED_LIBS=OFF ..</div><div class="line">...</div><div class="line">...</div><div class="line">make -j8</div><div class="line"># optional</div><div class="line"># make install</div></div><!-- fragment --> <h2>Install Leiningen </h2>
<p>Once you installed OpenCV with desktop java support the only other requirement is to install <a href="https://github.com/technomancy/leiningen">Leiningeng</a> which allows you to manage the entire life cycle of your CLJ projects.</p>
<p>The available <a href="https://github.com/technomancy/leiningen#installation">installation guide</a> is very easy to be followed:</p>
<ol type="1">
<li><a href="https://raw.github.com/technomancy/leiningen/stable/bin/lein">Download the script</a></li>
<li>Place it on your $PATH (cf./bin is a good choice if it is on your path.)</li>
<li>Set the script to be executable. (i.e. chmod 755/bin/lein).</li>
</ol>
<p>If you work on Windows, follow <a href="https://github.com/technomancy/leiningen#windows">this instruction</a></p>
<p>You now have both the OpenCV library and a fully installed basic Clojure environment. What is now needed is to configure the Clojure environment to interact with the OpenCV library.</p>
<h2>Install the localrepo Leiningen plugin </h2>
<p>The set of commands (tasks in Leiningen parlance) natively supported by Leiningen can be very easily extended by various plugins. One of them is the <a href="https://github.com/kumarshantanu/lein-localrepo">lein-localrepo</a> plugin which allows to install any jar lib as an artifact in the local maven repository of your machine (typically in the /.m2/repository directory of your username).</p>
<p>We're going to use this lein plugin to add to the local maven repository the opencv components needed by Java and Clojure to use the opencv lib.</p>
<p>Generally speaking, if you want to use a plugin on project base only, it can be added directly to a CLJ project created by lein.</p>
<p>Instead, when you want a plugin to be available to any CLJ project in your username space, you can add it to the profiles.clj in the/.lein/ directory.</p>
<p>The lein-localrepo plugin will be useful to me in other CLJ projects where I need to call native libs wrapped by a Java interface. So I decide to make it available to any CLJ project: </p><div class="fragment"><div class="line">mkdir ~/.lein</div></div><!-- fragment --><p> Create a file named profiles.clj in the/.lein directory and copy into it the following content: </p><div class="fragment"><div class="line">{:user {:plugins [[lein-localrepo &quot;0.5.2&quot;]]}}</div></div><!-- fragment --><p> Here we're saying that the version release "0.5.2" of the lein-localrepo plugin will be available to the :user profile for any CLJ project created by lein.</p>
<p>You do not need to do anything else to install the plugin because it will be automatically downloaded from a remote repository the very first time you issue any lein task.</p>
<h2>Install the java specific libs as local repository </h2>
<p>If you followed the standard documentation for installing OpenCV on your computer, you should find the following two libs under the directory where you built OpenCV:</p>
<ul>
<li>the build/bin/opencv-247.jar java lib</li>
<li>the build/lib/libopencv_java247.dylib native lib (or .so in you built OpenCV a GNU/Linux OS)</li>
</ul>
<p>They are the only opencv libs needed by the JVM to interact with OpenCV.</p>
<h3>Take apart the needed opencv libs</h3>
<p>Create a new directory to store in the above two libs. Start by copying into it the opencv-247.jar lib. </p><div class="fragment"><div class="line">cd ~/opt</div><div class="line">mkdir clj-opencv</div><div class="line">cd clj-opencv</div><div class="line">cp ~/opt/opencv/build/bin/opencv-247.jar .</div></div><!-- fragment --><p> First lib done.</p>
<p>Now, to be able to add the libopencv_java247.dylib shared native lib to the local maven repository, we first need to package it as a jar file.</p>
<p>The native lib has to be copied into a directories layout which mimics the names of your operating system and architecture. I'm using a Mac OS X with a X86 64 bit architecture. So my layout will be the following: </p><div class="fragment"><div class="line">mkdir -p native/macosx/x86_64</div></div><!-- fragment --><p> Copy into the x86_64 directory the libopencv_java247.dylib lib. </p><div class="fragment"><div class="line">cp ~/opt/opencv/build/lib/libopencv_java247.dylib native/macosx/x86_64/</div></div><!-- fragment --><p> If you're running OpenCV from a different OS/Architecture pair, here is a summary of the mapping you can choose from. </p><div class="fragment"><div class="line">OS</div><div class="line"></div><div class="line">Mac OS X -&gt; macosx</div><div class="line">Windows  -&gt; windows</div><div class="line">Linux    -&gt; linux</div><div class="line">SunOS    -&gt; solaris</div><div class="line"></div><div class="line">Architectures</div><div class="line"></div><div class="line">amd64    -&gt; x86_64</div><div class="line">x86_64   -&gt; x86_64</div><div class="line">x86      -&gt; x86</div><div class="line">i386     -&gt; x86</div><div class="line">arm      -&gt; arm</div><div class="line">sparc    -&gt; sparc</div></div><!-- fragment --> <h3>Package the native lib as a jar</h3>
<p>Next you need to package the native lib in a jar file by using the jar command to create a new jar file from a directory. </p><div class="fragment"><div class="line">jar -cMf opencv-native-247.jar native</div></div><!-- fragment --><p> Note that ehe M option instructs the jar command to not create a MANIFEST file for the artifact.</p>
<p>Your directories layout should look like the following: </p><div class="fragment"><div class="line">tree</div><div class="line">.</div><div class="line">|__ native</div><div class="line">|   |__ macosx</div><div class="line">|       |__ x86_64</div><div class="line">|           |__ libopencv_java247.dylib</div><div class="line">|</div><div class="line">|__ opencv-247.jar</div><div class="line">|__ opencv-native-247.jar</div><div class="line"></div><div class="line">3 directories, 3 files</div></div><!-- fragment --> <h3>Locally install the jars</h3>
<p>We are now ready to add the two jars as artifacts to the local maven repository with the help of the lein-localrepo plugin. </p><div class="fragment"><div class="line">lein localrepo install opencv-247.jar opencv/opencv 2.4.7</div></div><!-- fragment --><p> Here the localrepo install task creates the 2.4.7. release of the opencv/opencv maven artifact from the opencv-247.jar lib and then installs it into the local maven repository. The opencv/opencv artifact will then be available to any maven compliant project (Leiningen is internally based on maven).</p>
<p>Do the same thing with the native lib previously wrapped in a new jar file. </p><div class="fragment"><div class="line">lein localrepo install opencv-native-247.jar opencv/opencv-native 2.4.7</div></div><!-- fragment --><p> Note that the groupId, opencv, of the two artifacts is the same. We are now ready to create a new CLJ project to start interacting with OpenCV.</p>
<h3>Create a project</h3>
<p>Create a new CLJ project by using the lein new task from the terminal. </p><div class="fragment"><div class="line"># cd in the directory where you work with your development projects (e.g. ~/devel)</div><div class="line">lein new simple-sample</div><div class="line">Generating a project called simple-sample based on the &#39;default&#39; template.</div><div class="line">To see other templates (app, lein plugin, etc), try `lein help new`.</div></div><!-- fragment --><p> The above task creates the following simple-sample directories layout: </p><div class="fragment"><div class="line">tree simple-sample/</div><div class="line">simple-sample/</div><div class="line">|__ LICENSE</div><div class="line">|__ README.md</div><div class="line">|__ doc</div><div class="line">|   |__ intro.md</div><div class="line">|</div><div class="line">|__ project.clj</div><div class="line">|__ resources</div><div class="line">|__ src</div><div class="line">|   |__ simple_sample</div><div class="line">|       |__ core.clj</div><div class="line">|__ test</div><div class="line">    |__ simple_sample</div><div class="line">        |__ core_test.clj</div><div class="line"></div><div class="line">6 directories, 6 files</div></div><!-- fragment --><p> We need to add the two opencv artifacts as dependencies of the newly created project. Open the project.clj and modify its dependencies section as follows: </p><div class="fragment"><div class="line">(defproject simple-sample &quot;0.1.0-SNAPSHOT&quot;</div><div class="line">description &quot;FIXME: write description&quot;</div><div class="line">url &quot;http://example.com/FIXME&quot;</div><div class="line">license {:name &quot;Eclipse Public License&quot;</div><div class="line">url &quot;http://www.eclipse.org/legal/epl-v10.html&quot;}</div><div class="line">dependencies [[org.clojure/clojure &quot;1.5.1&quot;]</div><div class="line">                 [opencv/opencv &quot;2.4.7&quot;] ; added line</div><div class="line">                 [opencv/opencv-native &quot;2.4.7&quot;]]) ;added line</div></div><!-- fragment --><p> Note that The Clojure Programming Language is a jar artifact too. This is why Clojure is called an hosted language.</p>
<p>To verify that everything went right issue the lein deps task. The very first time you run a lein task it will take sometime to download all the required dependencies before executing the task itself. </p><div class="fragment"><div class="line">cd simple-sample</div><div class="line">lein deps</div><div class="line">...</div></div><!-- fragment --><p> The deps task reads and merges from the project.clj and the/.lein/profiles.clj files all the dependencies of the simple-sample project and verifies if they have already been cached in the local maven repository. If the task returns without messages about not being able to retrieve the two new artifacts your installation is correct, otherwise go back and double check that you did everything right.</p>
<h3>REPLing with OpenCV</h3>
<p>Now cd in the simple-sample directory and issue the following lein task: </p><div class="fragment"><div class="line">cd simple-sample</div><div class="line">lein repl</div><div class="line">...</div><div class="line">...</div><div class="line">nREPL server started on port 50907 on host 127.0.0.1</div><div class="line">REPL-y 0.3.0</div><div class="line">Clojure 1.5.1</div><div class="line">    Docs: (doc function-name-here)</div><div class="line">          (find-doc &quot;part-of-name-here&quot;)</div><div class="line">  Source: (source function-name-here)</div><div class="line"> Javadoc: (javadoc java-object-or-class-here)</div><div class="line">    Exit: Control+D or (exit) or (quit)</div><div class="line"> Results: Stored in vars *1, *2, *3, an exception in *e</div><div class="line"></div><div class="line">user=&gt;</div></div><!-- fragment --><p> You can immediately interact with the REPL by issuing any CLJ expression to be evaluated. </p><div class="fragment"><div class="line">user=&gt; (+ 41 1)</div><div class="line">42</div><div class="line">user=&gt; (println &quot;Hello, OpenCV!&quot;)</div><div class="line">Hello, OpenCV!</div><div class="line">nil</div><div class="line">user=&gt; (defn foo [] (str &quot;bar&quot;))</div><div class="line">#&#39;user/foo</div><div class="line">user=&gt; (foo)</div><div class="line">&quot;bar&quot;</div></div><!-- fragment --><p> When ran from the home directory of a lein based project, even if the lein repl task automatically loads all the project dependencies, you still need to load the opencv native library to be able to interact with the OpenCV. </p><div class="fragment"><div class="line">user=&gt; (clojure.lang.RT/loadLibrary org.opencv.core.Core/NATIVE_LIBRARY_NAME)</div><div class="line">nil</div></div><!-- fragment --><p> Then you can start interacting with OpenCV by just referencing the fully qualified names of its classes.</p>
<dl class="section note"><dt>Note</dt><dd><a href="https://docs.opencv.org/master/javadoc/index.html">Here</a> you can find the full OpenCV Java API.</dd></dl>
<div class="fragment"><div class="line">user=&gt; (org.opencv.core.Point. 0 0)</div><div class="line">#&lt;Point {0.0, 0.0}&gt;</div></div><!-- fragment --><p> Here we created a two dimensions opencv Point instance. Even if all the java packages included within the java interface to OpenCV are immediately available from the CLJ REPL, it's very annoying to prefix the Point. instance constructors with the fully qualified package name.</p>
<p>Fortunately CLJ offer a very easy way to overcome this annoyance by directly importing the Point class. </p><div class="fragment"><div class="line">user=&gt; (import &#39;org.opencv.core.Point)</div><div class="line">org.opencv.core.Point</div><div class="line">user=&gt; (def p1 (Point. 0 0))</div><div class="line">#&#39;user/p1</div><div class="line">user=&gt; p1</div><div class="line">#&lt;Point {0.0, 0.0}&gt;</div><div class="line">user=&gt; (def p2 (Point. 100 100))</div><div class="line">#&#39;user/p2</div></div><!-- fragment --><p> We can even inspect the class of an instance and verify if the value of a symbol is an instance of a Point java class. </p><div class="fragment"><div class="line">user=&gt; (class p1)</div><div class="line">org.opencv.core.Point</div><div class="line">user=&gt; (instance? org.opencv.core.Point p1)</div><div class="line">true</div></div><!-- fragment --><p> If we now want to use the opencv Rect class to create a rectangle, we again have to fully qualify its constructor even if it leaves in the same org.opencv.core package of the Point class. </p><div class="fragment"><div class="line">user=&gt; (org.opencv.core.Rect. p1 p2)</div><div class="line">#&lt;Rect {0, 0, 100x100}&gt;</div></div><!-- fragment --><p> Again, the CLJ importing facilities is very handy and let you to map more symbols in one shot. </p><div class="fragment"><div class="line">user=&gt; (import &#39;[org.opencv.core Point Rect Size])</div><div class="line">org.opencv.core.Size</div><div class="line">user=&gt; (def r1 (Rect. p1 p2))</div><div class="line">#&#39;user/r1</div><div class="line">user=&gt; r1</div><div class="line">#&lt;Rect {0, 0, 100x100}&gt;</div><div class="line">user=&gt; (class r1)</div><div class="line">org.opencv.core.Rect</div><div class="line">user=&gt; (instance? org.opencv.core.Rect r1)</div><div class="line">true</div><div class="line">user=&gt; (Size. 100 100)</div><div class="line">#&lt;Size 100x100&gt;</div><div class="line">user=&gt; (def sq-100 (Size. 100 100))</div><div class="line">#&#39;user/sq-100</div><div class="line">user=&gt; (class sq-100)</div><div class="line">org.opencv.core.Size</div><div class="line">user=&gt; (instance? org.opencv.core.Size sq-100)</div><div class="line">true</div></div><!-- fragment --><p> Obviously you can call methods on instances as well. </p><div class="fragment"><div class="line">user=&gt; (.area r1)</div><div class="line">10000.0</div><div class="line">user=&gt; (.area sq-100)</div><div class="line">10000.0</div></div><!-- fragment --><p> Or modify the value of a member field. </p><div class="fragment"><div class="line">user=&gt; (set! (.x p1) 10)</div><div class="line">10</div><div class="line">user=&gt; p1</div><div class="line">#&lt;Point {10.0, 0.0}&gt;</div><div class="line">user=&gt; (set! (.width sq-100) 10)</div><div class="line">10</div><div class="line">user=&gt; (set! (.height sq-100) 10)</div><div class="line">10</div><div class="line">user=&gt; (.area sq-100)</div><div class="line">100.0</div></div><!-- fragment --><p> If you find yourself not remembering a OpenCV class behavior, the REPL gives you the opportunity to easily search the corresponding javadoc documentation: </p><div class="fragment"><div class="line">user=&gt; (javadoc Rect)</div><div class="line">&quot;http://www.google.com/search?btnI=I%27m%20Feeling%20Lucky&amp;q=allinurl:org/opencv/core/Rect.html&quot;</div></div><!-- fragment --> <h3>Mimic the OpenCV Java Tutorial Sample in the REPL</h3>
<p>Let's now try to port to Clojure the <a class="el" href="../../d9/d52/tutorial_java_dev_intro.html">OpenCV Java tutorial sample</a>. Instead of writing it in a source file we're going to evaluate it at the REPL.</p>
<p>Following is the original Java source code of the cited sample. </p><div class="fragment"><div class="line"><span class="keyword">import</span> org.opencv.core.Mat;</div><div class="line"><span class="keyword">import</span> org.opencv.core.CvType;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Scalar;</div><div class="line"></div><div class="line"><span class="keyword">class </span>SimpleSample {</div><div class="line"></div><div class="line">  <span class="keyword">static</span>{ System.loadLibrary(<span class="stringliteral">&quot;opencv_java244&quot;</span>); }</div><div class="line"></div><div class="line">  <span class="keyword">public</span> <span class="keyword">static</span> <span class="keywordtype">void</span> main(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">    Mat m = <span class="keyword">new</span> Mat(5, 10, CvType.CV_8UC1, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0));</div><div class="line">    System.out.println(<span class="stringliteral">&quot;OpenCV Mat: &quot;</span> + m);</div><div class="line">    Mat mr1 = m.row(1);</div><div class="line">    mr1.setTo(<span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(1));</div><div class="line">    Mat mc5 = m.col(5);</div><div class="line">    mc5.setTo(<span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(5));</div><div class="line">    System.out.println(<span class="stringliteral">&quot;OpenCV Mat data:\n&quot;</span> + m.dump());</div><div class="line">  }</div><div class="line"></div><div class="line">}</div></div><!-- fragment --><h3>Add injections to the project</h3>
<p>Before start coding, we'd like to eliminate the boring need of interactively loading the native opencv lib any time we start a new REPL to interact with it.</p>
<p>First, stop the REPL by evaluating the (exit) expression at the REPL prompt. </p><div class="fragment"><div class="line">user=&gt; (exit)</div><div class="line">Bye for now!</div></div><!-- fragment --><p> Then open your project.clj file and edit it as follows: </p><div class="fragment"><div class="line">(defproject simple-sample &quot;0.1.0-SNAPSHOT&quot;</div><div class="line">  ...</div><div class="line">injections [(clojure.lang.RT/loadLibrary org.opencv.core.Core/NATIVE_LIBRARY_NAME)])</div></div><!-- fragment --><p> Here we're saying to load the opencv native lib anytime we run the REPL in such a way that we have not anymore to remember to manually do it.</p>
<p>Rerun the lein repl task </p><div class="fragment"><div class="line">lein repl</div><div class="line">nREPL server started on port 51645 on host 127.0.0.1</div><div class="line">REPL-y 0.3.0</div><div class="line">Clojure 1.5.1</div><div class="line">    Docs: (doc function-name-here)</div><div class="line">          (find-doc &quot;part-of-name-here&quot;)</div><div class="line">  Source: (source function-name-here)</div><div class="line"> Javadoc: (javadoc java-object-or-class-here)</div><div class="line">    Exit: Control+D or (exit) or (quit)</div><div class="line"> Results: Stored in vars *1, *2, *3, an exception in *e</div><div class="line"></div><div class="line">user=&gt;</div></div><!-- fragment --><p> Import the interested OpenCV java interfaces. </p><div class="fragment"><div class="line">user=&gt; (import &#39;[org.opencv.core Mat CvType Scalar])</div><div class="line">org.opencv.core.Scalar</div></div><!-- fragment --><p> We're going to mimic almost verbatim the original OpenCV java tutorial to:</p>
<ul>
<li>create a 5x10 matrix with all its elements initialized to 0</li>
<li>change the value of every element of the second row to 1</li>
<li>change the value of every element of the 6th column to 5</li>
<li>print the content of the obtained matrix</li>
</ul>
<div class="fragment"><div class="line">user=&gt; (def m (Mat. 5 10 CvType/CV_8UC1 (Scalar. 0 0)))</div><div class="line">#&#39;user/m</div><div class="line">user=&gt; (def mr1 (.row m 1))</div><div class="line">#&#39;user/mr1</div><div class="line">user=&gt; (.setTo mr1 (Scalar. 1 0))</div><div class="line">#&lt;Mat Mat [ 1*10*CV_8UC1, isCont=true, isSubmat=true, nativeObj=0x7fc9dac49880, dataAddr=0x7fc9d9c98d5a ]&gt;</div><div class="line">user=&gt; (def mc5 (.col m 5))</div><div class="line">#&#39;user/mc5</div><div class="line">user=&gt; (.setTo mc5 (Scalar. 5 0))</div><div class="line">#&lt;Mat Mat [ 5*1*CV_8UC1, isCont=false, isSubmat=true, nativeObj=0x7fc9d9c995a0, dataAddr=0x7fc9d9c98d55 ]&gt;</div><div class="line">user=&gt; (println (.dump m))</div><div class="line">[0, 0, 0, 0, 0, 5, 0, 0, 0, 0;</div><div class="line">  1, 1, 1, 1, 1, 5, 1, 1, 1, 1;</div><div class="line">  0, 0, 0, 0, 0, 5, 0, 0, 0, 0;</div><div class="line">  0, 0, 0, 0, 0, 5, 0, 0, 0, 0;</div><div class="line">  0, 0, 0, 0, 0, 5, 0, 0, 0, 0]</div><div class="line">nil</div></div><!-- fragment --><p>If you are accustomed to a functional language all those abused and mutating nouns are going to irritate your preference for verbs. Even if the CLJ interop syntax is very handy and complete, there is still an impedance mismatch between any OOP language and any FP language (bein Scala a mixed paradigms programming language).</p>
<p>To exit the REPL type (exit), ctr-D or (quit) at the REPL prompt. </p><div class="fragment"><div class="line">user=&gt; (exit)</div><div class="line">Bye for now!</div></div><!-- fragment --><h3>Interactively load and blur an image</h3>
<p>In the next sample you will learn how to interactively load and blur and image from the REPL by using the following OpenCV methods:</p>
<ul>
<li>the imread static method from the Highgui class to read an image from a file</li>
<li>the imwrite static method from the Highgui class to write an image to a file</li>
<li>the GaussianBlur static method from the Imgproc class to apply to blur the original image</li>
</ul>
<p>We're also going to use the Mat class which is returned from the imread method and accepted as the main argument to both the GaussianBlur and the imwrite methods.</p>
<h3>Add an image to the project</h3>
<p>First we want to add an image file to a newly create directory for storing static resources of the project.</p>
<div class="image">
<img src="../../lena.png" alt="lena.png"/>
</div>
 <div class="fragment"><div class="line">mkdir -p resources/images</div><div class="line">cp ~/opt/opencv/doc/tutorials/introduction/desktop_java/images/lena.png resource/images/</div></div><!-- fragment --> <h3>Read the image</h3>
<p>Now launch the REPL as usual and start by importing all the OpenCV classes we're going to use: </p><div class="fragment"><div class="line">lein repl</div><div class="line">nREPL server started on port 50624 on host 127.0.0.1</div><div class="line">REPL-y 0.3.0</div><div class="line">Clojure 1.5.1</div><div class="line">    Docs: (doc function-name-here)</div><div class="line">          (find-doc &quot;part-of-name-here&quot;)</div><div class="line">  Source: (source function-name-here)</div><div class="line"> Javadoc: (javadoc java-object-or-class-here)</div><div class="line">    Exit: Control+D or (exit) or (quit)</div><div class="line"> Results: Stored in vars *1, *2, *3, an exception in *e</div><div class="line"></div><div class="line">user=&gt; (import &#39;[org.opencv.core Mat Size CvType]</div><div class="line">               &#39;[org.opencv.imgcodecs Imgcodecs]</div><div class="line">               &#39;[org.opencv.imgproc Imgproc])</div><div class="line">org.opencv.imgproc.Imgproc</div></div><!-- fragment --><p> Now read the image from the resources/images/lena.png file. </p><div class="fragment"><div class="line">user=&gt; (def lena (Highgui/imread &quot;resources/images/lena.png&quot;))</div><div class="line">#&#39;user/lena</div><div class="line">user=&gt; lena</div><div class="line">#&lt;Mat Mat [ 512*512*CV_8UC3, isCont=true, isSubmat=false, nativeObj=0x7f9ab3054c40, dataAddr=0x19fea9010 ]&gt;</div></div><!-- fragment --><p> As you see, by simply evaluating the lena symbol we know that lena.png is a 512x512 matrix of CV_8UC3 elements type. Let's create a new Mat instance of the same dimensions and elements type. </p><div class="fragment"><div class="line">user=&gt; (def blurred (Mat. 512 512 CvType/CV_8UC3))</div><div class="line">#&#39;user/blurred</div><div class="line">user=&gt;</div></div><!-- fragment --><p> Now apply a GaussianBlur filter using lena as the source matrix and blurred as the destination matrix. </p><div class="fragment"><div class="line">user=&gt; (Imgproc/GaussianBlur lena blurred (Size. 5 5) 3 3)</div><div class="line">nil</div></div><!-- fragment --><p> As a last step just save the blurred matrix in a new image file. </p><div class="fragment"><div class="line">user=&gt; (Highgui/imwrite &quot;resources/images/blurred.png&quot; blurred)</div><div class="line">true</div><div class="line">user=&gt; (exit)</div><div class="line">Bye for now!</div></div><!-- fragment --><p> Following is the new blurred image of Lena.</p>
<div class="image">
<img src="../../blurred.png" alt="blurred.png"/>
</div>
<h2>Next Steps </h2>
<p>This tutorial only introduces the very basic environment set up to be able to interact with OpenCV in a CLJ REPL.</p>
<p>I recommend any Clojure newbie to read the <a href="http://clojure.org/java_interop">Clojure Java Interop chapter</a> to get all you need to know to interoperate with any plain java lib that has not been wrapped in Clojure to make it usable in a more idiomatic and functional way within Clojure.</p>
<p>The OpenCV Java API does not wrap the highgui module functionalities depending on Qt (e.g. namedWindow and imshow. If you want to create windows and show images into them while interacting with OpenCV from the REPL, at the moment you're left at your own. You could use Java Swing to fill the gap.</p>
<h3>License</h3>
<p>Copyright © 2013 Giacomo (Mimmo) Cosenza aka Magomimmo</p>
<p>Distributed under the BSD 3-clause License. </p>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:36 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
